import Vue from 'vue'
import VueRouter from 'vue-router'
import feedback from '../views/feedback/feedback.vue'
import home from '../views/home/home.vue'
import login from '../views/login/login.vue'
import city from '../views/city/city.vue'
Vue.use(VueRouter)

const routes = [
  // { path: '/', redirect: '/home' },
  {
    path: '/',
    component: home,
    children: [
      {
        path: '',
        component: feedback,
        meta: {
          keepAlive: true,
        }
      },
      {
        path: '/feedback',
        component: feedback,
        meta: {
          keepAlive: true,
        }
      },
      {
        path: '/record',
        // route level code-splitting
        // this generates a separate chunk (record.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "record" */ '../views/record/record.vue')
      }
    ]
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/city',
    component: city
  },

]


const router = new VueRouter({
  mode: "hash",
  routes
})


router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token");
  //如果没登录,都导向登录页
  if (!token) {
    if (to.path !== '/login') {
      if (to.query.cityname && to.query.url) {
        next({ path: `/login?id=${to.query.id}&url=${to.query.url}&cityname=${to.query.cityname}` })
      } else if (to.query.action == "houtai") {
        next();
      } else if (to.query.id && !to.query.url) {
        next({ path: `/login?id=${to.query.id}`})
      } else {
        next({ path: "/login" })
      }

    }
    else {
      next();
    }
  }
  else {
    next();
  }
})

export default router
